package com.zidian.study.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.student.trucks.R;
import com.zidian.study.adapter.ActionPeopleListAdapter;
import com.zidian.study.bean.ActionPeopleBean;
import com.zidian.study.globa.Constant;
import com.zidian.study.globa.ToolBarActivity;
import com.zidian.study.utils.GetUserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by GongCheng on 2016/11/23.
 */

public class ActionPeopleActivity extends ToolBarActivity {
    @Bind(R.id.action_people_list_rv)
    RecyclerView actionPeopleListRv;
    @Bind(R.id.people_count_tv)
    TextView peopleCountTv;

    private String activityId;
    private String count;
    private List<ActionPeopleBean.DataBean> dataList;
    private ActionPeopleListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_action_people);
        ButterKnife.bind(this);
        setTitle("查看报名人数");
        dataList = new ArrayList<>();
        activityId = getIntent().getStringExtra("activityId");
        count = getIntent().getStringExtra("count");

        peopleCountTv.setText(count);
        adapter = new ActionPeopleListAdapter();
        actionPeopleListRv.setLayoutManager(new LinearLayoutManager(this));
        actionPeopleListRv.setAdapter(adapter);
        getPeopleList();
    }

    private void getPeopleList() {
        String url = Constant.URL_GOAL + "activity/selectP";
        Map<String, String> map = new HashMap<>();
        map.put("activityId", activityId);
        map.put("studentId", GetUserInfo.getStudentId(context));
        map.put("token", GetUserInfo.getToken(context));


        OkHttpUtils.post().url(url).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Snackbar.make(actionPeopleListRv,"查询失败",Snackbar.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int code = jsonObject.getInt("code");
                    if (code == 200) {
                        Gson gson = new Gson();
                        ActionPeopleBean bean = gson.fromJson(response,ActionPeopleBean.class);
                        dataList.addAll(bean.getData());
                        adapter.setData(dataList);
                    }
                    else {
                        Snackbar.make(actionPeopleListRv,jsonObject.getString("message"),Snackbar.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
